package com.neuedu.springmvc.controller;

import com.neuedu.springmvc.VO;
import com.neuedu.springmvc.entity.Person;
import com.neuedu.springmvc.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 项目：springmvc-java4
 * 创建时间：  2022-03-23   9:21
 * 作者 :jshand
 * 描述 : 人员的
 */
@Controller
@RequestMapping("person")
public class PersonController {


    /**
     * http://localhost:8080/springmvc/queryById
     * @return
     */
    @RequestMapping("queryById")
    public ModelAndView queryById(){
        ModelAndView modelAndView = new ModelAndView();


        //设置视图名字（jsp路径）
//        modelAndView.setViewName("/WEB-INF/view/person/person_edit.jsp");
        //使用视图解析器
        modelAndView.setViewName("/person/person_edit");

        //设置数据
        Map person = new HashMap();
        person.put("id",1);
        person.put("name","张飞");
        person.put("address","燕赵");

        //request.setAttribute("person",person);
        modelAndView.addObject("person",person);

        return modelAndView;

    }


    /**
     * http://localhost:8080/springmvc/person/list
     * @return
     */
    @RequestMapping("/list")
    public String list(ModelMap model){
        //数据
        List list = new ArrayList();
        for (int i = 0; i < 10; i++) {
            Map person = new HashMap();
            person.put("id",i);
            person.put("name","张飞"+i);
            person.put("address","燕赵"+i);
            list.add(person);
        }
        model.addAttribute("list",list);

//        请求转发（内部跳转）
//        使用视图解析器默认前缀和后缀
        return "/person/person_list";
    }


    /**
     * 想要跳转到非标准前缀目录下的jsp
     * @param model
     * @return
     */
    @RequestMapping("/other")
    public String other(ModelMap model){
        //内部跳转非标注路径（不适用viewresolver的前缀和后缀）
        return "forward:/WEB-INF/otherview/other.jsp";
    }


    /**
     * 重定向
     * @return
     */
    @RequestMapping("/redirect")
    public String redirect(){

        return "redirect:/index.jsp";
    }


//    Struts 2x. Integer id;
    @RequestMapping("param1")
    public void param1(Integer id, @RequestParam(value="username",required=false) String name){
        System.out.println("id = " + id);
        System.out.println("name = " + name);
//        reutrn "aaa"
    }


    /**
     * http://localhost:8080/springmvc/person/param2?id=10&name=jjjjjj&address=aaddd
     * @param user
     */
    @RequestMapping("param2")
    public void param2(User user){
        System.out.println(user);
    }

    /**
     * http://localhost:8080/springmvc/person/param2?id=10&name=jjjjjj&address=aaddd&fav
     * @param user
     */
    @RequestMapping("param3")
    public void param3(User user, Person person){
        System.out.println(user);
        System.out.println(person);
    }

    /**
     * http://localhost:8080/springmvc/person/param4?person.id=10&person.name=jjjjjj&person.fav=ball&user.id=10&user.name=jjjjjj&user.address=hrb
     */
    @RequestMapping("param4")
    public void param4(VO vo){
        System.out.println(vo);
    }


    @RequestMapping("param5")
    public String param5(Integer[] userId,HttpServletRequest request){

//        String userId = request.getParameterValues("userId")
        for (Integer id : userId) {
            System.out.println("id = " + id);
        }
        return "forward:/index.jsp";
    }


    @RequestMapping("param6")
    public String param6(@RequestParam Map map){

        System.out.println("map = " + map);

        return "forward:/index.jsp";
    }


    @RequestMapping("param7")
    public String param7(VO vo,HttpServletRequest req) throws ParseException {

        Date time = null;

        String paramData = req.getParameter("time");

        time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(paramData);


        String strAge = req.getParameter("age");
        Integer id = Integer.parseInt("314");
        Double d = Double.parseDouble("3.14");

        //2022-01-01
        //2022年01月01日
        //2022/01/01
        //03/22/2022
        //03/22 2022

        //编写转换器


        List<User> userList = vo.getUserList();

        for (User user : userList) {
            System.out.println("user = " + user);
        }

        return "forward:/index.jsp";
    }


    /**
     * http://localhost:8080/springmvc/person/param8?time=2022-01-01
     * @param user
     * @return
     * @throws ParseException
     */
    @RequestMapping("param8")
    public String param8(User user) throws ParseException {
        System.out.println("user = " + user);
        return "forward:/index.jsp";
    }

}
